package com.zhongyu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhongyu.mapper.CangkuKuCunMapper;
import com.zhongyu.mapper.KuCunBdMapper;
import com.zhongyu.mapper.OrderDetailMapper;
import com.zhongyu.mapper.OrderMapper;
import com.zhongyu.pojo.CangkuKucun;
import com.zhongyu.pojo.KucunBiandong;
import com.zhongyu.pojo.Order;
import com.zhongyu.pojo.OrderDetail;
import com.zhongyu.service.OrderDetailService;
import com.zhongyu.vo.ResultVo;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
@Service("OrderDetailService")
public class OrderDetailServicelmpl extends ServiceImpl<OrderDetailMapper, OrderDetail> implements OrderDetailService {
    @Resource
    private  OrderDetailMapper orderDetailMapper;
    @Resource
    private CangkuKuCunMapper cangkuKuCunMapper;
    @Resource
    private KuCunBdMapper kuCunBdMapper;
    @Resource
    private  OrderMapper orderMapper;



    @Override
    public List<OrderDetail> findById(Integer orderId) {
        return orderDetailMapper.findById(orderId);
    }

    @Override
    public    List<OrderDetail> findBydetailId1(Integer detailId) {
        return orderDetailMapper.findBydetailId1(detailId);
    }

    @Override
    public OrderDetail findBydetailId(Integer detailId) {
        return orderDetailMapper.findBydetailId(detailId);
    }

    @Override
    public void xsadd(Order order) {

    }

    @Override
    public void add(OrderDetail order) {

    }

    @SneakyThrows
    @Override
    public ResultVo doOutStore(Integer userId, Integer detailId, Integer cangkuId) {
        OrderDetail orderDetail = orderDetailMapper.findBydetailId(detailId);
        log.debug(String.valueOf(orderDetail));
        if(orderDetail.getState()!=0){
            return  ResultVo.failure("亲！该明细已经出库了，不能重复出库哦!");
        }else{
            //查询库存
            CangkuKucun cangkuKucun = new CangkuKucun();
            cangkuKucun.setGoodsId(orderDetail.getGoodsId());
            if(cangkuId==null){
                return ResultVo.failure("请选择仓库");
            }else {
                cangkuKucun.setCangkuId(cangkuId);
            }

            List<CangkuKucun> cangkuKucunlList = cangkuKuCunMapper.getList(cangkuKucun,cangkuKucun.getGoodsId(),cangkuKucun.getCangkuId());
            //商品仓库库存数量
            Integer num = -1;
            //如果存在库存，检查库存是否足够
            if(null != cangkuKucunlList && cangkuKucunlList.size() > 0){
                cangkuKucun = cangkuKucunlList.get(0);
                num = cangkuKucun.getShuliang().intValue()-orderDetail.getNum().intValue();
            }
            if(num >0){
                //库存充足，则更新库存数量
                cangkuKucun.setShuliang(num);
                cangkuKuCunMapper.updateById(cangkuKucun);

                //更新订单详情
                orderDetail.setState(1);
                orderDetail.setEndTime(LocalDateTime.now());
                orderDetail.setStorehouseId(cangkuId);
                orderDetail.setStockKeeper(userId);
                orderDetailMapper.updateById(orderDetail);
                //添加库存变更操作记录
                KucunBiandong kucunBiandong = new KucunBiandong();
                kucunBiandong.setUserid(userId);
                kucunBiandong.setGoodsId(orderDetail.getGoodsId());
                kucunBiandong.setNum(orderDetail.getNum());
                kucunBiandong.setOpertime(orderDetail.getEndTime());
                kucunBiandong.setCangkuId(cangkuId);
                kucunBiandong.setType("2");
                kuCunBdMapper.insert(kucunBiandong);


                //检查是否订单下的所有明细都已经出库
                OrderDetail queryParam  = new OrderDetail();
                Order orders = orderDetail.getOrder();
                queryParam .setOrder(orders);
                queryParam .setState(0);
                Integer count = orderDetailMapper.getCount(queryParam,queryParam.getState(), orderDetail.getOrderId());
                if(count==0){
                    //所有明细都已经出库，则更新订单状态为已出库
                    orders.setState(String.valueOf(1));
                    orders.setStockKeeper(userId);
                    orders.setStockTime(orderDetail.getEndTime());
                    orderMapper.updateById(orders);
                }
            }else if(num<0){
                //库存不足，则提示用户
                return  ResultVo.failure("库存不足,请及时补货" );
            }else {
                //库存充足，则更新库存数量
                cangkuKucun.setShuliang(num);
                cangkuKuCunMapper.updateById(cangkuKucun);

                //更新订单详情
                orderDetail.setState(1);
                orderDetail.setEndTime(LocalDateTime.now());
                orderDetail.setStorehouseId(cangkuId);
                orderDetail.setStockKeeper(userId);
                orderDetailMapper.updateById(orderDetail);
                //添加库存变更操作记录
                KucunBiandong kucunBiandong = new KucunBiandong();
                kucunBiandong.setUserid(userId);
                kucunBiandong.setGoodsId(orderDetail.getGoodsId());
                kucunBiandong.setNum(orderDetail.getNum());
                kucunBiandong.setOpertime(orderDetail.getEndTime());
                kucunBiandong.setCangkuId(cangkuId);
                kucunBiandong.setType("2");
                kuCunBdMapper.insert(kucunBiandong);


                //检查是否订单下的所有明细都已经出库
                OrderDetail queryParam  = new OrderDetail();
                Order orders = orderDetail.getOrder();
                queryParam .setOrder(orders);
                queryParam .setState(0);
                Integer count = orderDetailMapper.getCount(queryParam,queryParam.getState(), orderDetail.getOrderId());
                if(count==0){
                    //所有明细都已经出库，则更新订单状态为已出库
                    orders.setState(String.valueOf(1));
                    orders.setStockKeeper(userId);
                    orders.setStockTime(orderDetail.getEndTime());
                    orderMapper.updateById(orders);
                }
                return  ResultVo.failure("库存已清零,请及时补货" );
            }
        }
        return  ResultVo.success("出库成功");
    }
    @SneakyThrows
    @Override
    public ResultVo doStore(Integer userId, Integer detailId, Integer cangkuId) {
        OrderDetail orderDetail = orderDetailMapper.findBydetailId(detailId);
        if(orderDetail.getState()!=0){
            return  ResultVo.failure("亲！该明细已经入库了，不能重复入库哦!");
        }else{
            //查询库存
            CangkuKucun cangkuKucun = new CangkuKucun();
            cangkuKucun.setGoodsId(orderDetail.getGoodsId());
            if(cangkuId==null){
                return ResultVo.failure("请选择仓库");
            }else {
                cangkuKucun.setCangkuId(cangkuId);
            }
            List<CangkuKucun> cangkuKucunlList = cangkuKuCunMapper.getList(cangkuKucun,cangkuKucun.getGoodsId(),cangkuKucun.getCangkuId());
            //商品仓库库存数量
            Integer num = -1;
            //如果存在库存，检查库存是否足够
            if(null != cangkuKucunlList && cangkuKucunlList.size() > 0){
                cangkuKucun = cangkuKucunlList.get(0);
                num = cangkuKucun.getShuliang().intValue()+orderDetail.getNum().intValue();
                //库存充足，则更新库存数量
                cangkuKucun.setShuliang(num);
                cangkuKuCunMapper.updateById(cangkuKucun);

                //更新订单详情
                orderDetail.setState(3);
                orderDetail.setEndTime(LocalDateTime.now());
                orderDetail.setStorehouseId(cangkuId);
                orderDetail.setStockKeeper(userId);
                orderDetailMapper.updateById(orderDetail);
                //添加库存变更操作记录
                KucunBiandong kucunBiandong = new KucunBiandong();
                kucunBiandong.setUserid(userId);
                kucunBiandong.setGoodsId(orderDetail.getGoodsId());
                kucunBiandong.setNum(orderDetail.getNum());
                kucunBiandong.setOpertime(orderDetail.getEndTime());
                kucunBiandong.setCangkuId(cangkuId);
                kucunBiandong.setType("1");
                kuCunBdMapper.insert(kucunBiandong);


                //检查是否订单下的所有明细都已经出库
                OrderDetail queryParam  = new OrderDetail();
                Order orders = orderDetail.getOrder();
                queryParam .setOrder(orders);
                queryParam .setState(0);
                Integer count = orderDetailMapper.getCount(queryParam,queryParam.getState(), orderDetail.getOrderId());
                if(count==0){
                    //所有明细都已经出库，则更新订单状态为已入库
                    orders.setState(String.valueOf(3));
                    orders.setStockKeeper(userId);
                    orders.setStockTime(orderDetail.getEndTime());
                    orderMapper.updateById(orders);
                }
            }else{
                num =0+orderDetail.getNum().intValue();
                cangkuKucun.setShuliang(num);
                cangkuKucun.setGoodsId(orderDetail.getGoodsId());

                cangkuKucun.setCangkuId(cangkuId);
                cangkuKuCunMapper.insert(cangkuKucun);

                //更新订单详情
                orderDetail.setState(3);
                orderDetail.setEndTime(LocalDateTime.now());
                orderDetail.setStorehouseId(cangkuId);
                orderDetail.setStockKeeper(userId);
                orderDetailMapper.updateById(orderDetail);
                //添加库存变更操作记录
                KucunBiandong kucunBiandong = new KucunBiandong();
                kucunBiandong.setUserid(userId);
                kucunBiandong.setGoodsId(orderDetail.getGoodsId());
                kucunBiandong.setNum(orderDetail.getNum());
                kucunBiandong.setOpertime(orderDetail.getEndTime());
                kucunBiandong.setCangkuId(cangkuId);
                kucunBiandong.setType("1");
                kuCunBdMapper.insert(kucunBiandong);


                //检查是否订单下的所有明细都已经出库
                OrderDetail queryParam  = new OrderDetail();
                Order orders = orderDetail.getOrder();
                queryParam .setOrder(orders);
                queryParam .setState(0);
                Integer count = orderDetailMapper.getCount(queryParam,queryParam.getState(), orderDetail.getOrderId());
                if(count==0){
                    //所有明细都已经出库，则更新订单状态为已入库
                    orders.setState(String.valueOf(3));
                    orders.setStockKeeper(userId);
                    orders.setStockTime(orderDetail.getEndTime());
                    orderMapper.updateById(orders);
                }
            }
        }
        return  ResultVo.success("入库成功");
    }

   /* @Override
    public void del(Integer id) {

    }*/

    @Override
    public void update(OrderDetail order) {
        orderDetailMapper.updateById(order);
    }
}
